<?php
/**
 * @package framework
 */

class MediaImage extends Image implements MediaInterface
{

	public $name = "";
	private $_config;
	private $_thumbPrefix = "thumb_";

	public function setConfig($opts)
	{
		$this->_config = $opts;
	}

	public function getType()
	{
		return $this->_config['type'];
	}

    public function getSize()
    {
        return $this->fileSize();
    }

	public function getDimensions()
	{
		return $this->_config['width']." x ".$this->_config['height'];
	}

    public function printHTML()
    {
		echo $this->getHTML($this->_config);
    }

    public function printHTMLThumb()
    {
		echo $this->getHTMLThumb($this->_config);
    }

    public function getHTMLThumb($opts=array())
    {
        $opts["attr"] = $opts["attr"]? $opts["attr"] : $this->attr;
        $opts["style"] = $opts["style"]? $opts["style"] : $this->style;
        return file_exists($this->getPathThumb()) ? "<img src=\"". $this->getUrlThumb() ."\" ". $opts["attr"] ." ". ($opts["style"]? "style=\"". $opts["style"] ."\"" : "") .">" : "";
    }

    public function getPathThumb()
    {
        return $this->dirPath ."/". $this->_thumbPrefix . $this->filename;
    }

    public function getUrlThumb()
    {
        return $this->urlPath ."/". $this->_thumbPrefix . $this->filename;
    }

    // Chequeos simples para evitar archivos que no sean imagenes:
    public function isValidImage($filePost)
    {
        // Chequeo dimension:
        list($w, $h, $t) = getimagesize($filePost['tmp_name']);

        // Chequeo tipo (1-IMAGETYPE_GIF   2-IMAGETYPE_JPEG   3-IMAGETYPE_PNG):
        if($t!=1 && $t!=2 && $t!=3)
		{	
			$this->_error("Unsuported type");
			return false;
		}
		
        if(!is_numeric($w) || !is_numeric($h))
		{
			$this->_error("Wrong size dimension.");
			return false;
		}

        // Chequeo extension:
        $ext = strtolower(pathinfo($filePost['name'], PATHINFO_EXTENSION));
        if(!in_array($ext, array( $this->_config['type'] )))
		{
			$this->_error("The file uploaded must be a ".$this->_config['type']." file.");
			return false;
		}

        return true;
    }

	public function delete()
	{
		if(file_exists($this->getPathThumb())) unlink($this->getPathThumb());
		parent::delete();
	}

    public function update($filePost)
    {
        if ($filePost['name'])
        {
            $isError = false;
            if (!$this->isValidImage($filePost)) return false;

            // I catch the posted file
            if ($this->catchPost($filePost)!=UPLOAD_ERR_OK)
                return $this->_error($this->getErrorMessage());

            // I see if the posted file is ok and a supported file format
            if (!$this->savePost())
                return $this->_error($this->getErrorMessage());

            // Saving the main image
            if($this->_config['fitInArea'])
            {
                if (!$this->fitInArea($this->_config['width'], $this->_config['height'], $this->_config['quality']))
                    return $this->_error($this->getErrorMessage());
            }
            else if(!$this->_config['avoidResizing'])
            {
                if (!$this->resize($this->_config['width'], $this->_config['height'], $this->_config['quality']))
                    return $this->_error($this->getErrorMessage());
            }

            // Saving Image Thumbnail
            if ($this->_config['withThumb'])
            {
                if($this->_config['thumbFitInArea'])
                {
                    $resizeCoef = 1;
                    if(($this->width > $this->_config['thumbWidth']) || ($this->height > $this->_config['thumbHeight']))
                    {
                        $resizeX = (float)$this->_config['thumbWidth']/$this->width;
                        $resizeY = (float)$this->_config['thumbHeight']/$this->height;
                        $resizeCoef = ($resizeX < $resizeY) ? $resizeX : $resizeY;
                    }
                    $thumbWidth = $this->width * $resizeCoef;
                    $thumbHeigth = $this->height * $resizeCoef;
                }
                else
                {
                    $thumbWidth = $this->_config['thumbWidth'];
                    $thumbHeigth = $this->_config['thumbHeight'];
                }

                if (!($this->thumbObj = $this->createThumbnail($thumbWidth, $thumbHeigth, $this->_thumbPrefix.$this->filename, '', false, $this->_config['quality'])))
                {
                    $this->_error($this->thumbObj->getErrorMessage());
                    return 0;
                }
            }
            return 1;
        }
        return 0;
    }




}

?>